package com.supermap.dataservice.webservice;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Aqua 2017-5-26
 * @Filename ParseXml.java
 * @Description 解析xml
 * @email 1301584033@qq.com
 */

public class ParseXml {

    /**
     * 解析xml并返回
     *
     * @param result 返回的xml类容
     * @return String
     */
    public static List<String> getXml(String result) throws Exception {
        Document doc = DocumentHelper.parseText(result);
        Element root = doc.getRootElement();
        Element resultElement = (Element) ((Element) ((Element) root.elements().get(0)).elements().get(0)).elements().get(0);
        Iterator iter = resultElement.elementIterator();
        StringBuilder stringBuilder = new StringBuilder();
        while (iter.hasNext()) {
            Element itemElement = (Element) iter.next();
            stringBuilder.append(itemElement.getText() + ",");
        }
        String[] str = stringBuilder.toString().split(",");
        List<String> imagelist = new ArrayList<String>();
        for (int i = 1; i <= str.length; i++) {
            if (str.length > i) {
                if (str[i].indexOf("http") != -1) {
                    imagelist.add(str[i].substring(str[i].indexOf("http"), str[i].length()));
                }
            }
        }

        return imagelist;
    }

    /**
     * 解析xml并返回
     *
     * @param result 返回的xml类容（里面的时间）
     * @return String
     */
    public static List<String> getXmlTime(String result) throws Exception {
        Document doc = DocumentHelper.parseText(result);
        Element root = doc.getRootElement();
        Element resultElement = (Element) ((Element) ((Element) root.elements().get(0)).elements().get(0)).elements().get(0);
        Iterator iter = resultElement.elementIterator();
        StringBuilder stringBuilder = new StringBuilder();
        while (iter.hasNext()) {
            Element itemElement = (Element) iter.next();
            stringBuilder.append(itemElement.getText() + ",");
        }
        String[] str = stringBuilder.toString().split(",");
        List<String> imagelist = new ArrayList<String>();
        for (int i = 1; i <= str.length; i++) {
            if (str.length > i) {
                if (str[i].indexOf("http") != -1) {
                    imagelist.add(str[i].substring(0, str[i].indexOf("http")));
                }
            }
        }

        return imagelist;
    }


}